package com.estar.app.business.mobileLogin.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.estar.app.appsrvyyabx.mobile.domain.DataVO;
import com.estar.app.appsrvyyabx.mobile.domain.VOBase;
import com.estar.app.appsrvyyabx.mobile.domain.vo.DangerTimesVO;
import com.estar.platform.caseInfo.entity.TmblCarInfoEntity;

@Component("checkDangerTimesUtil")
@Scope("prototype")
public class CheckDangerTimesUtil{
	@Autowired
	private SystemService systemService;
	
	private static final Logger logger = Logger.getLogger(CheckDangerTimesUtil.class);
	
	/**
	 * 1-n辆车出险次数查询
	 * @param dataVO
	 * @return
	 */
	public DataVO getDangerTimes(DataVO dataVO){
		DataVO returnVo = new DataVO();
		try{
			//获取需要查询的车辆信息
			List carInfos = dataVO.getList();
			TmblCarInfoEntity tcie = null;
			String carNo = null;
			String driverName = null;
			String phone = null;
			
			//组装查询结果
			StringBuilder result = new StringBuilder();
			//查询sql
			String sql = "";
			Long carNoCount = 0L;
			Long driverNameCount = 0L;
			Long phoneCount = 0L;
			
			for (int i=0;i<carInfos.size();i++) {
				
				tcie = (TmblCarInfoEntity) carInfos.get(i);
				
				carNo = tcie.getCarNo();
				driverName = tcie.getDriverNme();
				phone = tcie.getPhone();
				if(StringUtils.isNotBlank(carNo)){ // 且有重复数据的去掉（ 违规人不一定是车主）
					sql = "select count(1) from tmbl_car_info where t_create_tm >= add_months(sysdate,-3) and T_CAR_NO ='"+carNo+"'";
					carNoCount = (systemService.getCountForJdbc(sql));
					if(carNoCount >= 3 && result.indexOf(carNo) < 0)
						result.append("该").append(carNo).append("最近三个月出过险").append(carNoCount).append("次;");
				}
				if(StringUtils.isNotBlank(driverName)){
					sql = "select count(1) from tmbl_car_info where t_create_tm >= add_months(sysdate,-3) and t_driver_nme ='"+driverName+"'";
					driverNameCount = (systemService.getCountForJdbc(sql));
					if(driverNameCount >= 3 && result.indexOf(driverName) < 0)
						result.append("该").append(driverName).append("最近三个月出过险").append(driverNameCount).append("次;");
				}
				if(StringUtils.isNotBlank(phone)){
					sql = "select count(1) from tmbl_car_info where t_create_tm >= add_months(sysdate,-3) and t_phone ='"+phone+"'";
					phoneCount = (systemService.getCountForJdbc(sql));
					if(phoneCount >= 3 && result.indexOf(phone) < 0)
						result.append("该").append(phone).append("最近三个月出过险").append(phoneCount).append("次;");
				}
				
/*				if(StringUtils.isNotBlank(carNo)){ // 且有重复数据的去掉（ 违规人不一定是车主）
					sql = "select count(1) from tmbl_car_info where t_create_tm >= add_months(sysdate,-3) and T_CAR_NO ='"+carNo+"'";
					carNoCount = (systemService.getCountForJdbc(sql));
					if(carNoCount >= 3)
						result.append("该").append(carNo).append("最近三个月出过险").append(carNoCount).append("次");
				}
				if(StringUtils.isNotBlank(driverName)){
					sql = "select count(1) from tmbl_car_info where t_create_tm >= add_months(sysdate,-3) and t_driver_nme ='"+driverName+"'";
					driverNameCount = (systemService.getCountForJdbc(sql));
					if(driverNameCount >= 3)
						result.append("该").append(driverName).append("最近三个月出过险").append(driverNameCount).append("次");
				}
				if(StringUtils.isNotBlank(phone)){
					sql = "select count(1) from tmbl_car_info where t_create_tm >= add_months(sysdate,-3) and t_phone ='"+phone+"'";
					phoneCount = (systemService.getCountForJdbc(sql));
					if(phoneCount >= 3)
						result.append("该").append(phone).append("最近三个月出过险").append(phoneCount).append("次");
				}*/
			}
			//设置返回查询结果
			returnVo.setResultCde("1");
			returnVo.setMessage(result.toString());
			
			
		}catch (Exception e) {
			returnVo.setResultCde("0");
			returnVo.setMessage("出险次数查询失败");
			logger.error("系统异常：",e);
			e.printStackTrace();
		}
		return returnVo;
	}

}
